package com.hhxy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.annotation.ControllerLog;
import com.hhxy.handler.exception.CustomException;
import com.hhxy.service.ArticleService;
import com.hhxy.utils.response.AppHttpCodeEnum;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author ghp
 * @date 2023/4/25
 * @title
 * @description
 */
@Api(tags = {"前台接口", "文章管理"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping("/article")
public class FeArticleController {

    @Resource
    private ArticleService articleService;

    @GetMapping("/hotArticleList")
    @ApiOperation(value = "查询热门文章", notes = "热门文章为阅读量排名前十的文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ControllerLog(businessName = "查询热门文章")
    public ResponseResult hotArticleList() {
        return articleService.hotArticleList();
    }

    @GetMapping("/articleList")
    @ApiOperation(value = "分页条件查看文章")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataTypeClass = Integer.class, required = true),
            @ApiImplicitParam(name = "categoryId", value = "分类id", dataTypeClass = Long.class),
    })
    @ControllerLog(businessName = "分页条件查看文章")
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        // TODO 参数校验
        return articleService.articleList(pageNum, pageSize, categoryId);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看文章详情")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "文章id", dataTypeClass = Long.class)
    @ControllerLog(businessName = "根据id查看文章详情")
    public ResponseResult getArticleDetail(@PathVariable Long id) {
        return articleService.getArticleDetail(id);
    }

    @PutMapping("/updateViewCount/{id}")
    @ApiOperation(value = "更新文章访问量")
    @ApiOperationSupport(author = "ghp", order = 1)
    @ApiImplicitParam(name = "id", value = "文章id", dataTypeClass = Long.class, required = true)
    public ResponseResult updateViewCount(@PathVariable Long id){
        if (Objects.isNull(id)){
            throw new CustomException(AppHttpCodeEnum.CLIENT_ERROR);
        }
        return articleService.updateViewCount(id);
    }

}
